#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'kang'

from selenium import webdriver
from selenium.webdriver.common.by import By
import datetime
import time

driver = webdriver.Chrome()
driver.maximize_window()

'''*****去登录*****'''
def login(name, pws):
    #打开淘宝登录页，并进行扫码登录
    driver.get("https://www.taobao.com")
    time.sleep(0.5)
    if driver.find_element(By.LINK_TEXT, "亲，请登录"):
        driver.find_element(By.LINK_TEXT, "亲，请登录").click()

    # if driver.find_element(By.CLASS_NAME, "corner-icon-view view-type-qrcode"):
    #     driver.find_element(By.CLASS_NAME, "corner-icon-view view-type-qrcode").click()

    input(name, pws)
    time.sleep(10)

    driver.get("https://cart.taobao.com/cart.htm")
    time.sleep(3)

    #点击购物车里面的全选按钮
    if driver.find_element(By.ID, "J_SelectAll1"):
        driver.find_element(By.ID, "J_SelectAll1").click()

    now = datetime.datetime.now()
    print('login success:', now.strftime('%Y-%m-%d %H:%M:%S:%f'))


'''------账号登录------'''
def input(uname, pwd):
    time.sleep(1)
    # 选择密码登录
    if driver.find_element(By.CLASS_NAME, "password-login-tab-item"):
        driver.find_element(By.CLASS_NAME, "password-login-tab-item").click()
    time.sleep(0.5)
    #输入用户名
    if driver.find_element(By.NAME, "fm-login-id"):
        for name in uname:
            driver.find_element(By.NAME, "fm-login-id").send_keys(name)
            time.sleep(0.1)

    # 输入用户名
    if driver.find_element(By.NAME, "fm-login-password"):
        for pw in pwd:
            driver.find_element(By.NAME, "fm-login-password").send_keys(pw)
            time.sleep(0.1)
    time.sleep(1)

    #点击登录按钮
    if driver.find_element(By.CLASS_NAME, "fm-button fm-submit sms-login"):
        driver.find_element(By.CLASS_NAME, "fm-button fm-submit sms-login").click()
    time.sleep(1)

'''*****购物车选购付款'''
def buy(buytime):
    while True:
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S:%f')
        print('当前时间：' + now)
        #对比时间 时间到的话就结算
        if now > buytime:
            try:
                #点击结算按钮
                if driver.find_element(By.ID, "J_Go"):
                    driver.find_element(By.ID, "J_Go").click()
                # driver.find_element(By.LINK_TEXT, "提交订单").click()
            except:
                time.sleep(0.1)
        print(now)
        time.sleep(0.1)



if __name__ == "__main__":
    # times = input("请输入抢购时间：")
    # 时间格式："2020-09-09 11:20:00.000000"
    login("liukang", "123456")
    buy("2021-12-23 10:10:00.000000")


